package com.atguigu.service;

import com.atguigu.DAO.impl.BookDAO;
import com.atguigu.DAO.impl.BookDAOImple;
import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;

import java.util.List;

public class BookServiceImpl implements BookService {
    private BookDAO bookDAO=new BookDAOImple();
    @Override
    public void addBook(Book book) {
        bookDAO.addBook(book);
    }

    @Override
    public void deleteBookbyId(Integer id) {
        bookDAO.deleteBookbyId(id);
    }

    @Override
    public void updateBook(Book book) {
        bookDAO.updateBook(book);
    }

    @Override
    public Book queryBookById(Integer id) {
        Book book = bookDAO.queryBookById(id);
        return book;
    }

    @Override
    public List<Book> queryBooks() {
        List<Book> books = bookDAO.queryBooks();
        return books;
    }

    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page=new Page<>();
        //设置每页的数量
        page.setPagesize(pageSize);
        //求总记录数
        Integer pageTotalCount=bookDAO.queryForPageTotalCount();
        //设置总记录数
        page.setGetPageTotalCount(pageTotalCount);

        //求总页码
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);


        //设置页数
        page.setPageNo(pageNo);

        //求当前页数据的开始索引
        int begin=(page.getPageNo()-1)*pageSize;
        //求当前页数据
        List<Book> items=bookDAO.queryForpageItems(begin,pageSize);
        //设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Book> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Book> page=new Page<>();
        //设置每页的数量
        page.setPagesize(pageSize);
        //求总记录数
        Integer pageTotalCount=bookDAO.queryForPageTotalCountByPrice(min,max);
        //设置总记录数
        page.setGetPageTotalCount(pageTotalCount);

        //求总页码
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);


        //设置页数
        page.setPageNo(pageNo);

        //求当前页数据的开始索引
        int begin=(page.getPageNo()-1)*pageSize;
        //求当前页数据
        List<Book> items=bookDAO.queryForpageItemsByPrice(begin,pageSize,min,max);
        //设置当前页数据
        page.setItems(items);
        return page;
    }
}
